Hello! Welcome to my first internal training. In this markdown, we will learn about Social Network Analysis using various packages especially tidygraph (including igraph and ggraph). We’ll not only learn about the visualizing stuff but also the metrics. We’ll analyze Twitter network as our study case using rtweet package. After this internal training, I hope we will be able to do:

  • Analyze Ego Network from specific account/user
  • Analyze Competition Network from various similar account/industries
  • Analyze Information Activity Network from something that goes viral

Note: In 28 March 2019, Kak Eca already deliver a cool internal training about Twitter Interactions using Twinetverse . It has similar idea with this study, we both explain about building network from Twitter data. The difference in this study is we’re not only make visualization, but also calculate metrics to achieve the goals for every case (spoiler: we’ll do 3 SNA cases). We’ll also more focus to how to implement SNA and what it is. This study is theory-driven studies.

Background

Introduction

What is Social Network Analysis (SNA)
A social network is a structure composed of a set of actors, some of which are connected by a set of one or more relations. Social network analysis work at describing underlying patterns of social structure, explaining the impact of such patterns in behaviour and attitudes. Social network analysis has 4 main types of network metrics, namely:

  • Network Models: Describe how to model the relationship between users
  • Key Players: To identify the most influential users in the network based on different context
  • Tie Strength: To measure the strength of user’s relationship
  • Network Cohesion: To measure how cohesive entities in the networks towards network behavior.

So What? Why do we need them?
Humans are social beings. Even when you sleep, you’re still connected to everyone in the world by your smartphone. Your smartphone keep sending and receive information like weather information, incoming whatsapp messages, late night One Piece update, and social media notification from your favourite bias. We’re always connected and there’s network everywhere. Somehow, some smart dudes behind the famous Small World Theory found something from the network that quite exciting.

Did you know you only seperates by six step from your favourite person in the world? We are able to quantify what-so-called network and can be implemented in many fields. In this study we’ll only focus on identify network metrics with key player as the expected output (see the 4 main types of network metrics above). Here’s some implementation of SNA to enlight your knowledge about SNA a bit: (will put reference links later)

  • Business:
    • Social media segmentation
    • Information spreading through network (for marketing purpose)
    • Identify prominent person of a society (for identify best endorsment)
    • Mapping potential customer
    • Mapping tourism flow
  • Non-business:
    • Analyzing how something goes viral in social media
    • Identify diseases spread
    • Word embedding stuff
    • Implementation of small-world theory and Six degree separation of Kevin Bacon
    • will add more~

Let’s Begin !

Prerequisites

We’ll crawl Twitter data using Twitter’s rest API. Thus, we need authentication to use the API. To access the API, you will need to create a Twitter Developer Account here: https://developer.twitter.com/en (make sure you already have Twitter account). Creating Twitter developer account is simple and tend to be fast but it depend to how you describe what you will do with the API.

Good news! recent update of rtweet allows you to interact with Twitter API without create your own Twitter developer account. But it’s better if you have one because it gives you more stability and permissions. If you need further explanation, you can head over rtweet’s official website here.

In this study, we’ll crawl Twitter data without using access token as crendentials. but if things are going bad, i provide some access token we can use to crawl the data. Note: Due to rate limit, and most of use will use it at the same time, please use the token wisely.

## <Token>
## <oauth_endpoint>
##  request:   https://api.twitter.com/oauth/request_token
##  authorize: https://api.twitter.com/oauth/authenticate
##  access:    https://api.twitter.com/oauth/access_token
## <oauth_app> Automated Twitter SNA
##   key:    A5csjkdrS24vJ5ktiKYtgasFY
##   secret: <hidden>
## <credentials> oauth_token, oauth_token_secret
## ---

Case Objectives

In this study, we try to solve this 3 cases:

  • Analyze Ego Network from @TeamAlgoritma Twitter account
    • Visualize top cluster from TeamAlgoritma mutual account
    • find out which account has the potential to spread information widely
    • Calculate the metrics, and find out who is the key player in TeamAlgoritma network
  • Analyze Competition Network from @kfc_id, @mcd_id, @wendys, @phd account
    • Visualize the whole competition network
    • Identify fanbase cluster in each account
    • Calculate the metrics, and find out who is the key player in the whole network
  • Analyze Activity Network given random keyword(s) or hashtag
    • Visualize the activity/information network
    • find out which cluster talk about which topic regarding the keyword
    • Calculate the metrics, and find out who is the key player in the whole conversation network

Case 1: TeamAlgoritma Ego Network

Ego network is just a simply The concept indicates the amount of all the nodes (here, destinations) to which an ego/node is directly connected and includes all of the ties among nodes in a network. You take any random username/company/person you want to analyze, gather all their neighborhood, and analyze it. sometimes you’ll find interesting pattern like this person has a lot of different communities and none of them are look alike, or you can also found a person who can spread information most widely around around your target-person network.

Here’s the step to do this case:
1. Gather TeamAlgoritma detail Twitter data
2. Gather all TeamAlgoritma followers
3. From the follower, filter to active account only and gather their follower and following
4. Create Mutual data from following and follower data
5. Build communities, Calculate SNA metrics, and identify which user is important
6. Visualize the ego network

Picture 1: Ego network

Picture 1: Ego network

Gather @TeamAlgoritma data

Gather TeamAlgoritma followers

## 75000 followers!

TeamAlgoritma Twitter account has 342 followers (on 15 May 2020). We need to gather all of their follower and following but Twitter rest API has (kinda stingy) limitation.

We can only gather 15 users (both following and follower) and 5k retrieved for every 15 minutes, so you can imagine if we want to retrieve thousand of them.. In order to minimize the time consumption, we need to filter the users to the active users only. The criteria of ‘active users’ is depend on your data. You need to lookup which kind of users your follower is and build your own criteria. In this case, the top 8 of Algoritma’s follower is a media account. Account like ‘btekno’ and ‘machinelearnflx’ only repost link to their own media and never retweet other account tweets. So if our goal is to map the potential information spreading around TeamAlgoritma ego network, we need to exclude them for that reason.

After a long inspection, i propose several criteria for filtering active account: Followers_count > 100 and < 6000, following_count > 75, favourites_count > 10, and create a new twit at least 2 months ago. I also want to exclude protected account because we actually cant do anything about it, we can’t gather their folloing and followers.

Gather TeamAlgoritma follower’s follower

I build a loop function to gather follower from a list. Actually, we also can gather the follower with this simple code

But we want to minimize the total user we want to retrieve (n parameter). i build a simple function to retrieve half of the followers if they have more than 1500 followers, and 75% followers if they have less than 1500

We also want to avoid SSL/TLS bug while we gather the followers. Sometimes when you reach the rate limit, the loop tend to crash and stop running. To avoid that, i order the loop to sleep every 5 gathered account (it’s not always solve the problem, but it way much better)

After gathering, bind the list to dataframe, convert the username to user_id ny left_join from active_fol data, and build clean data frame without NA

The loop need a looong time to be done. To speed up our progress, i already gather the followers and we’ll use it for analysis.

Gather TeamAlgoritma follower’s following

Same like beofre, we build loop function to gather the following. in rtweet package, following is also called as friend.

As you can see, friends_count is way more higher than followers_count. Thus, we need to specify how many user we want to retrieve (n parameter). We want to minimze it, i change flt_n function to gather only 40% if they have more than 2k following, and 65% if less than 2k.

The loop is also a bit different. instead of list, we store the data to dataframe. get_friends() function gives 2 column as their output; friend list and the querry. we can easily just row bind them.

This loop are also take a long time to run. Again, to speed up our progress, we will use following data i already gathered.

We need to make sure all unique active user in algo_friend is availiable in algo_following and vice versa

Create Mutual dataframe

Now we have both following and follower data. We need to build ‘mutual’ data to make sure the network is a strong two-side-connection network. Mutual is my terms of people who follow each other. we can found that by: split algo_friend data by every unique active_user, then we find every account in following column that also appear in algo_follower$follower. The presence in both column indicates the user are following each other

phew we finisihed data gathering step! next we’ll jump into SNA process

Build nodes, edges, and graph dataframe

A network consists of nodes and edges. nodes (also called as vertices) indicates every unique object in network and edges is a relation between nodes (object). We’ll build nodes dataframe from every unique account in algo_mutual df. and edges dataframe that contains pair of accounts, we can use algo_mutual df for that.

after that, we can simply create graph dataframe using graph_from_data_frame function from igraph package.

Build communities and calculate metrics

I need to remind you we’ll do the analysis using tidygraph style. There are lots different code style to build a network but i found tidygraph package is the easiest. tidygraph are just wrappers for igraph packages.

igraph code example:

Create communities using group_walktrap() algorithm, and calculate lots of metrics using tidygraph style

## Warning in closeness(graph = graph, vids = V(graph), mode = mode, weights =
## weights, : At centrality.c:2784 :closeness centrality is not well-defined for
## disconnected graphs
## # A tbl_graph: 14541 nodes and 15843 edges
## #
## # An undirected multigraph with 3 components
## #
## # Node Data: 14,541 x 6 (active)
##   name                community degree_c betweenness_c closeness_c     eigen
##   <chr>               <fct>        <dbl>         <dbl>       <dbl>     <dbl>
## 1 35167068            3               20      0.0247       0.00775 0.000186 
## 2 2196972205          3                7      0.00503      0.00774 0.0000861
## 3 97163422            3                9      0.00386      0.00774 0.0000622
## 4 1233994338922684416 3               23      0.0627       0.00776 0.000432 
## 5 882769518061395968  3                5      0.000288     0.00772 0.0000465
## 6 573357589           3                2      0            0.00771 0.0000274
## # ... with 1.454e+04 more rows
## #
## # Edge Data: 15,843 x 2
##    from    to
##   <int> <int>
## 1     1  6123
## 2     2  6123
## 3     3  6123
## # ... with 1.584e+04 more rows

We can easily convert it to dataframe using as.data.frame() function. We need to this to specify who is the key player in TeamAlgoritma ego network

Graph Metrics

Before we make a conclusion from the table above, let’s take a time to learn what’s the idea behind those metrics. We’ll build network from Algoritma Product Team as dummy network to make the explanation easier, and just to inform you how SNA works in real case

Degree Centrality

The easiest centrality among them all. Its just how many ties that a node has. A connection between nodes are seperated by 2 types: Directed and Undirected. Directed is a relationship between nodes that the edges has direction. Undirected indicates two-way relationship, it doesnt has any direction. Directed network also separated in 2 type based on its direction, namely: indegree and outdegree. The calculation for directed and undirected are kinda different but it have a same idea: how many nodes is connected to a node.

Closeness centrality

The closeness centrality of a node is the average length of the shortest path between the node and all other nodes in the graph. Thus the more central a node is, the closer it is to all other nodes. \[C(i) = \frac{N-1}{\sum_{j}d(j,i)}\] \(d(j,i)\) is the distance between vertices \(j\) and \(i\). This centrality divide total number of nodes minus 1(\(N-1\)) by total number of every shorthest path between one node to every node in the graph.

Betweenness centrality

Betweenness centrality quantifies the number of times a node acts as a bridge along the shortest path between two other nodes/groups.
\[C_{B}(v) = \sum_{ij}\frac{\sigma_{ij}(v)}{\sigma_{ij}}\] Where \(\sigma_{ij}\) is total number of shortest paths from node \(x\) to node \(y\) and \(\sigma_{ij}(v)\) is the number of those paths that pass through \(v\)

Eigenvector centrality

Eigenvector centrality is a measure of the influence of a node in a network. The relative score that are assigned to the nodes in the network are based on the concept that connections to high-scoring contributes more to the score of the node in question than equal connections to low-scoring nodes. This amazing link will help you with the calculation.

if \(A\) is the adjency matrix of a graph and \(\lambda\) is the largest eigenvalue of \(A\) and \(x\) is the corresponding eigenvector then \(Ax = \lambda x\). it can be transformed to \(x = \frac{1}{\lambda}Ax\). where \(Ax\) can be defined \(\sum_{j=1}^{N}A_{i,j}x_{j}\) therefore: \[C_{E}(i) = x_{i} = \frac{1}{\lambda}\sum_{j=1}^{N}A_{i,j}x_{j}\]

Community and Modularity

Building community in graph theory is a bit different than clustering in machine learning.igraph package implements a number of community detection methods, community structure detection algorithms try to find dense subgraphs in directed or undirected graphs, by optimizing some criteria, and usually using heuristics. Community detection algorithm like group_walktrap(), group_fast_greedy(), and group_louvain() has their own way to create communities in the network. One of the common use community detection algorithm is group_walktrap(). This function tries to find densely connected subgraphs, also called communities in a graph via random walks. The idea is that short random walks tend to stay in the same community.

Modularity in the other hand, is a measure of how good the division is, or how separated are the different vertex types from each other \[Q = \frac{1}{2m}\sum_{ij}(A_{ij}-\frac{k_{i}k_{j}}{2m})\delta(c_{i},c_{j})\] here \(m\) is the number of edges, \(A_{ij}\) is the element of the \(A\) adjacency matrix in row \(i\) and column \(j\), \(k_{i}\) is the degree of \(i\), \(k_{j}\) is the degree of \(j\), \(c_{i}\) is the type (or component) of \(i\), \(c_{j}\) that of \(j\), and \(\delta(c_{i},c_{j})\) is the Kronecker delta, which returns 1 if the operands are equal and 0 otherwise. In summary, networks with high modularity have dense connections between the nodes within community but sparse connections between nodes in different community

Now let’s check if this network has high or low modularity score

## [1] 0.06248927

low modularity score indicates the community in the network actually don’t have much difference. There are dense connections between nodes in both community. The community member might be different depends on what algorithm you use. you can try different algorithm and compare them using compare() function. By default, compare() returns a score by its Variance Information (method = "vi), which counts whether or not any two vertices are members of the same community. A lower score means that the two community structures are more similar

## [1] 0.7552945

Identify prominent user in the network

So at this point, i hope you understand the concept of graph, nodes & edges, centrality, community & modularity and how to use it. We will move back to our Twitter network. We alredy convert the table_graph to data frame. Last thing we need to do is to find top account in each centrality and pull the key player

Key player is a terms for the most influential users in the network based on different context. ‘Different context’ in this case is different centrality metrics. Each centrality have different use and intepretation, user that appear in the top of most centrality will be considered as the key player of the whole network.

From the table above, account “1049333510505291778” appear in most centrality. That account has the most degree in the network (high degree) but also surrounded by important person (high eigenvector). Thus, we can conclude that user “1049333510505291778” is the key player of TeamAlgoritma Twitter ego network.

Let’s see who he/she is: